package com.pxl.springbootinit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pxl.springbootinit.common.BaseResponse;
import com.pxl.springbootinit.common.ErrorCode;
import com.pxl.springbootinit.common.ResultUtils;
import com.pxl.springbootinit.exception.BusinessException;
import com.pxl.springbootinit.model.dto.task.TaskClaimForm;
import com.pxl.springbootinit.model.dto.task.TaskForm;
import com.pxl.springbootinit.model.entity.Task;
import com.pxl.springbootinit.model.entity.TaskClaim;
import com.pxl.springbootinit.model.entity.User;
import com.pxl.springbootinit.model.vo.TaskVO;
import com.pxl.springbootinit.service.TaskClaimService;
import com.pxl.springbootinit.service.TaskService;
import com.pxl.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author MADAO
 * @create 2024 - 12 - 20 14:08
 */
@Slf4j
@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    private TaskService taskService;

    @Resource
    private TaskClaimService taskClaimService;

    @Resource
    private UserService userService;

    /**
     * 任务发布
     * @param taskForm
     * @return
     */
    @PostMapping("/publish")
    public BaseResponse<Boolean> publishTask(@RequestBody TaskForm taskForm) {
        log.info("publishTask:{}",taskForm);
        return ResultUtils.success(taskService.publishTask(taskForm));
    }

    /**
     * 根据ID删除任务
     * @param id
     * @return
     */
    @PostMapping("/remove/{id}")
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Boolean> removeTask(@PathVariable("id") Long id) {
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<TaskClaim> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id",id);
        return ResultUtils.success(taskService.removeById(id) && taskClaimService.remove(queryWrapper));
    }

    /**
     * 修改任务信息
     * @param taskForm
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTask(@RequestBody TaskForm taskForm) {
        if (taskForm == null || taskForm.getId() == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Task task = new Task();
        BeanUtils.copyProperties(taskForm,task);
        return ResultUtils.success(taskService.updateById(task));
    }


    /**
     * 分页查询任务
     * @param taskForm
     * @return
     */
    @GetMapping("/page")
    public BaseResponse<Page<TaskVO>> pageTask(TaskForm taskForm) {
        log.info("pageTask:{}",taskForm);
        return ResultUtils.success(taskService.pageTask(taskForm));
    }

    @GetMapping("/{id}")
    public BaseResponse<TaskVO> getById(@PathVariable("id") Long id) {
        return ResultUtils.success(taskService.getTaskVO(taskService.getById(id)));
    }

    @GetMapping("/handle-review/page")
    public BaseResponse<Page<TaskVO>> pageTaskClaim(TaskForm taskForm,HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);

        return ResultUtils.success(taskService.handleReviewPage(taskForm,loginUser.getId()));
    }

    /**
     * 任务状态追踪
     * @param id
     * @return
     */
    @PostMapping("/trace/{id}")
    public BaseResponse<TaskVO> traceTask(@PathVariable("id") Long id) {
        return ResultUtils.success(taskService.traceTask(id));
    }


    @PostMapping("/finish/{id}")
    public BaseResponse<Boolean> finishTask(@PathVariable("id") Long id, HttpServletRequest request)  {
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        return ResultUtils.success(taskService.finishTask(id,loginUser.getId()));
    }


    // region 管理员操作
    /**
     * 返回系统总用户数量
     * @return
     */
    @GetMapping("/admin/count")
    public BaseResponse<Long> getUserCount() {
        Long count = taskService.count();
        return ResultUtils.success(count);
    }

    /**
     *  管理员分页查询任务
     * @param taskForm
     * @return
     */
    @GetMapping("/admin/page")
    public BaseResponse<Page<TaskVO>> pageAdminTask(TaskForm taskForm) {
        return ResultUtils.success(taskService.pageAdminTask(taskForm));
    }

    //endregion



}
